package com.iterator.service.web.spreadManager;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iterator.bean.web.pojo.tcy.Ad;
import com.iterator.bean.web.pojo.tcy.AdExample;
import com.iterator.bean.web.tcy.AdVO;
import com.iterator.mapper.web.spreadManager.AdMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author tean_cy
 * @date 2021/1/10 15:54
 * 广告管理模块
 */
@Service
public class AdServiceImpl implements AdService {

    @Autowired
    AdMapper adMapper;

    @Override
    public  AdVO list(Integer page, Integer limit, String name, String content, String sort, String order){
        AdExample adExample = new AdExample();
        AdExample.Criteria criteria = adExample.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(content)) {
            criteria.andContentLike("%" + content + "%");
        }
        criteria.andDeletedEqualTo(false);
        //排序 sord = addtime order升序或降序
        adExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page,limit);

        List<Ad> adList = adMapper.selectByExample(adExample);
        PageInfo<Ad> pageInfo = new PageInfo<>(adList);
        long total = pageInfo.getTotal();

        AdVO adVO = new AdVO(total,adList);
        return adVO;
    }

    @Override
    public Ad update(Ad adBO) {
        Date date = new Date();
        adBO.setUpdateTime(date);

        int i = adMapper.updateByPrimaryKeySelective(adBO);
        Ad adVO = null ;
        if (i != 0){
            adVO = adMapper.selectByPrimaryKey(adBO.getId());
        }
        return adVO;
    }

    @Override
    public int delete(Ad adBO) {
        adBO.setDeleted(true);
        int i = adMapper.updateByPrimaryKeySelective(adBO);
        return i;
    }


    @Override
    public Ad create(Ad adCreateBO) {
        Date date = new Date();
        adCreateBO.setUpdateTime(date);
        adCreateBO.setAddTime(date);
        int i = adMapper.insertSelective(adCreateBO);
        if (i != 0){
        return adCreateBO;
        }
        return null;
    }
}
